package com.reform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.reform.pojo.common.Department;
import com.reform.pojo.project.NumProject;
import com.reform.mapper.NumProjectMapper;
import com.reform.pojo.phase.ViewBatch;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IDepartmentService;
import com.reform.service.INumProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.service.IViewBatchService;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-08-06
 */
@Service
public class NumProjectServiceImpl extends ServiceImpl<NumProjectMapper, NumProject> implements INumProjectService {

    @Autowired
    NumProjectMapper numProjectMapper;
    @Autowired
    INumProjectService numProjectService;
    @Autowired
    IDepartmentService departmentService;
    @Autowired
    IViewBatchService viewBatchService;

    @Override
    public Result getAllNumProjectByBatchId(String batchId) {
        return Result.success(ResultCode.SUCCESS, numProjectMapper.selectList(new QueryWrapper<NumProject>().eq("batch_id", batchId)));
    }

    @Override
    public Result setNumProject(Integer numProId, String maxPronum) {
        UpdateWrapper<NumProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("num_pro_id", numProId).set("max_pronum", maxPronum);
        if (numProjectMapper.update(null, updateWrapper) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }


    @Override
    public Result initNumProject(String batchId) {
        List<NumProject> latelyNumProject = numProjectService.getLatelyNumProject();
        if (VerifycUtils.verifyNullOrEmptyUtils(latelyNumProject) && latelyNumProject.size() != 0) {
            for (NumProject numProject : latelyNumProject) {
                numProject.setNumProId(null);
                numProject.setBatchId(batchId);
                numProjectMapper.insert(numProject);
            }
        } else {
            List<Department> authDepartments = departmentService.getAuthDepartment();
            for (Department authDepartment : authDepartments) {
                NumProject numProject = new NumProject();
                numProject.setBatchId(batchId);
                numProject.setDepId(authDepartment.getDepId());
                numProject.setDepName(authDepartment.getDepName());
                numProjectMapper.insert(numProject);
            }
        }
        return Result.success(ResultCode.SUCCESS);
    }


    @Override
    public Result addNumProject(Integer dep_id, String dep_name) throws SQLException {
        int res = 0;
        //获取batchId
        List<String> batchIdList = viewBatchService.getBatchId();
        //未所有批次添加一个numproject
        for (String batchId : batchIdList) {
            NumProject numProject = new NumProject();
            numProject.setBatchId(batchId);
            numProject.setDepId(dep_id);
            numProject.setDepName(dep_name);
            numProject.setMaxPronum("0");
            res += numProjectMapper.insert(numProject);
        }
        if (res == batchIdList.size()) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result deleteNumProject(Integer dep_id) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        map.put("dep_id", dep_id);
        if (numProjectMapper.deleteByMap(map) > 0) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result setNumProjects(String numProIds, String maxPronums) {
        String[] numProIdSplit = numProIds.split(",");
        String[] maxPronumSplit = maxPronums.split(",");
        int res = 0;
        for (int i = 0; i < numProIdSplit.length; i++) {
            UpdateWrapper<NumProject> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("num_pro_id", numProIdSplit[i]).set("max_pronum", maxPronumSplit[i]);
            res += numProjectMapper.update(null, updateWrapper);
        }
        if (res == numProIdSplit.length) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public List<NumProject> getLatelyNumProject() {
        //获取最近一次的批次编号
        ViewBatch latelyBatchInfo = viewBatchService.getLatelyBatchInfo();
        if (VerifycUtils.verifyNullOrEmptyUtils(latelyBatchInfo))
            return numProjectMapper.selectList(new QueryWrapper<NumProject>().eq("batch_id", latelyBatchInfo.getBatchId()));
        return null;
    }

    @Override
    public Result editDepartment(Integer numProId, Integer isEmpower) {
        UpdateWrapper<NumProject> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("num_pro_id", numProId).set("is_empower", isEmpower);
        if (numProjectMapper.update(null, updateWrapper) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public List<String> getBatchIdByDepId(Integer depId) {
        return numProjectMapper.getBatchIdByDepId(depId);
    }
}
